<template>
  <el-dialog :title="title[index]" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
    <div class="zzc">
      <el-steps :active="active">
        <el-step title="步骤 1" description="数据模型基础信息"></el-step>
        <el-step title="步骤 2" description="字段信息"></el-step>
      </el-steps>
      <br />
      <br />
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <div v-show="active == 1">
          <el-row>
            <el-col :span="12">
              <el-form-item label="英文名称" prop="tableName">
                <el-input v-model="form.tableName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="中文名称" prop="tableComment">
                <el-input v-model="form.tableComment"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="数据目录" prop="directoryId">
                <el-cascader ref="cascaderHandle1" v-model="form.directoryId" :options="treeList"
                  :props="{ value: 'id', checkStrictly: true }" clearable></el-cascader>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="数据类型" prop="elementType">
                <el-cascader ref="cascaderHandle2" v-model="form.elementType" :options="typeoptions"
                  :props="{ checkStrictly: true }" clearable></el-cascader>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="模型类型" prop="modelType">
                <el-cascader ref="cascaderHandle3" v-model="form.modelType" :options="modelTypeoptions"
                  :props="{ checkStrictly: true }" clearable></el-cascader>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label=" 生效开始时间" prop="effectiveStartTime">
                <el-date-picker v-model="form.effectiveStartTime" type="date" placeholder="选择日期"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="生效结束时间:">
                <el-date-picker v-model="form.effectiveEndTime" type="date" placeholder="无期限"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="安全等级:" prop="securityoptions">
                <el-select v-model="form.tableSecurityLevel" placeholder="请选择">
                  <el-option v-for="item in securityoptions" :key="item.value" :label="item.label"
                    :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="相关实体对象:">
                <el-input v-model="form.entity"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row></el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="描述：">
                <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="form.description"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item>
            <el-button @click="
              off()
            root()
            active = 1
              ">取消
            </el-button>
            <el-button type="primary" @click="nextClick()">下一步</el-button>
          </el-form-item>
        </div>
        <div v-show="active == 2">
          <el-table :data="columnModelEntities" style="width: 100%" max-height="250">
            <el-table-column fixed label="字段英文名称" width="150" prop="columnName">
              <template slot-scope="scope">
                <!--                <el-form-item  :prop=" 'form.' + scope.$index + '.columnName'">-->
                <el-input v-model="columnModelEntities[scope.$index].columnName" style="width: 100%" type="text"
                  placeholder="请输入"></el-input>

                <!-- </el-form-item>-->
              </template>
            </el-table-column>
            <el-table-column label="中文名称" width="150">
              <template slot-scope="scope">
                <el-input v-model="columnModelEntities[scope.$index].columnComment" style="width: 100%" type="text"
                  placeholder="请输入"></el-input>
              </template>
            </el-table-column>
            <el-table-column label="字段类型" width="150">
              <template slot-scope="scope">
                <el-select v-model="columnModelEntities[scope.$index].columnType" placeholder="请选择">
                  <el-option v-for="item in ziduanoptions" :key="item.value" :label="item.label"
                    :value="item.value"></el-option>
                </el-select>
              </template>
            </el-table-column>

            <el-table-column label="长度" width="150">
              <template slot-scope="scope">
                <el-input v-model="columnModelEntities[scope.$index].dataLength" style="width: 100%" type="text"
                  placeholder="请输入"></el-input>
              </template>
            </el-table-column>

            <el-table-column label="主键" width="150">
              <template slot-scope="scope">
                <el-select v-model="columnModelEntities[scope.$index].columnKey" placeholder="请选择">
                  <el-option v-for="item in majoroptions" :key="item.value" :label="item.label"
                    :value="item.value"></el-option>
                </el-select>
              </template>
            </el-table-column>

            <el-table-column label="外键" width="150">
              <template slot-scope="scope">
                <el-select v-model="columnModelEntities[scope.$index].forginId" placeholder="请选择">
                  <el-option v-for="item in majoroptions" :key="item.value" :label="item.label"
                    :value="item.value"></el-option>
                </el-select>
              </template>
            </el-table-column>

            <el-table-column label="字段是否允许为空" width="150">
              <template slot-scope="scope">
                <el-select v-model="columnModelEntities[scope.$index].columnNullable" placeholder="请选择">
                  <el-option v-for="item in majoroptions" :key="item.value" :label="item.label"
                    :value="item.value"></el-option>
                </el-select>
              </template>
            </el-table-column>

            <el-table-column label="单位" width="150">
              <template slot-scope="scope">
                <el-input v-model="columnModelEntities[scope.$index].unit" style="width: 100%" type="text"
                  placeholder="请输入"></el-input>
              </template>
            </el-table-column>

            <el-table-column label="默认值" width="150">
              <template slot-scope="scope">
                <el-input v-model="columnModelEntities[scope.$index].dataDefault" style="width: 100%" type="text"
                  placeholder="请输入"></el-input>
              </template>
            </el-table-column>

            <el-table-column label="是否键值" width="150">
              <template slot-scope="scope">
                <el-select v-model="columnModelEntities[scope.$index].isKV" placeholder="请选择">
                  <el-option v-for="item in majoroptions" :key="item.value" :label="item.label"
                    :value="item.value"></el-option>
                </el-select>
              </template>
            </el-table-column>

            <el-table-column label="安全等级" width="150">
              <template slot-scope="scope">
                <el-select v-model="columnModelEntities[scope.$index].columnSecurityLevel" placeholder="请选择">
                  <el-option v-for="item in anquanions" :key="item.value" :label="item.label"
                    :value="item.value"></el-option>
                </el-select>
              </template>
            </el-table-column>

            <!--            <el-table-column label="关键字典表/字段" width="150">-->
            <!--              <template slot-scope="scope">-->
            <!--                <el-select v-model="value" placeholder="请选择">-->
            <!--                  <el-option v-for="item in options" :key="item.value" :label="item.label"-->
            <!--                             :value="item.value"></el-option>-->
            <!--                </el-select>-->
            <!--              </template>-->
            <!--            </el-table-column>-->
            <!--            <el-table-column label="值域标识字段" width="150">-->
            <!--              <template slot-scope="scope">-->
            <!--                <el-select v-model="value" placeholder="请选择">-->
            <!--                  <el-option v-for="item in options" :key="item.value" :label="item.label"-->
            <!--                             :value="item.value"></el-option>-->
            <!--                </el-select>-->
            <!--              </template>-->
            <!--            </el-table-column>-->
            <!--            <el-table-column label="值域标识值" width="150">-->
            <!--              <template slot-scope="scope">-->
            <!--                <el-input style="width: 100%" type="text" placeholder="请输入"></el-input>-->
            <!--              </template>-->
            <!--            </el-table-column>-->
            <el-table-column label="字典值" width="150">
              <template slot-scope="scope">
                <el-select v-model="columnModelEntities[scope.$index].dictV" placeholder="请选择">
                  <el-option v-for="item in options" :key="item.value" :label="item.label"
                    :value="item.value"></el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="数据类型" width="150">
              <template slot-scope="scope">
                <el-cascader v-model="columnModelEntities[scope.$index].dataType" :options="tabletypeoptions"
                  :props="{ checkStrictly: true }" clearable></el-cascader>
              </template>
            </el-table-column>
            <el-table-column label="数据层级" width="150">
              <template slot-scope="scope">
                <el-select v-model="columnModelEntities[scope.$index].dataLevel" placeholder="请选择">
                  <el-option v-for="item in options" :key="item.value" :label="item.label"
                    :value="item.value"></el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="数据格式" width="150">
              <template slot-scope="scope">
                <el-select v-model="columnModelEntities[scope.$index].dataFormat" placeholder="请选择">
                  <el-option v-for="item in options" :key="item.value" :label="item.label"
                    :value="item.value"></el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="字段说明" width="150">
              <template slot-scope="scope">
                <el-input type="textarea" :rows="2" placeholder="请输入内容"
                  v-model="columnModelEntities[scope.$index].columnDescription"></el-input>
              </template>
            </el-table-column>
            <el-table-column label="知识图谱显示" width="150">
              <template slot-scope="scope">
                <el-select v-model="columnModelEntities[scope.$index].mapKnowledge" placeholder="请选择">
                  <el-option v-for="item in majoroptions" :key="item.value" :label="item.label"
                    :value="item.value"></el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" align="center" width="120">
              <template slot-scope="scope">
                <i style="font-size: 30px" class="el-icon-circle-plus-outline" @click="adddataFun"></i>
                <i style="font-size: 30px" class="el-icon-remove-outline" @click="jiandataFun(scope.$index)"></i>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <el-form-item v-show="active == 2">
          <el-button @click="
            off()
          root()
          active = 1
            ">取消
          </el-button>
          <el-button @click="active = 1">上一步</el-button>
          <el-button type="primary" @click="submitForm('form')">保存</el-button>
          <el-button type="primary" @click="submit('form')">立即创建保存并发布</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
import { addtableLise, treeListApi, updatatableLise } from '@/api/masterdata/dataModelApi'
import dayjs from 'dayjs'

export default {
  props: ['dialogVisible', 'eitdValue', 'index'],
  data() {
    return {
      title: {
        add: '添加',
        edit: '编辑',
        copy: '复制',
      },
      treeList: [],
      securityoptions: [
        { label: '机密', value: 1 },
        { label: '秘密', value: 2 },
        { label: '内部公开', value: 3 },
        { label: '外部公开', value: 4 },
      ],
      modelTypeoptions: [
        {
          value: '字典表',
          label: '字典表',
        },
        {
          value: '关联表',
          label: '关联表',
          children: [
            {
              value: '业务数据表',
              label: '业务数据表',
            },
            {
              value: '基础数据表',
              label: '基础数据表',
            },
          ],
        },
      ],
      // modelTypeoptions: [{
      //   value: '关联表',
      //   label: '关联表',
      //   children: [{
      //     value: '业务数据表',
      //     label: '业务数据表'
      //   }, {
      //     value: '基础数据表',
      //     label: '基础数据表'
      //   }]
      // }, {
      //   value: '普通表',
      //   label: '普通表',
      //   children: [{
      //     value: '基础数据表',
      //     label: '基础数据表',
      //     children: [
      //       {
      //         value: '实体编码表',
      //         label: '实体编码表'
      //       },
      //       {
      //         value: '基础信息表',
      //         label: '基础信息表'
      //       }
      //     ]
      //   }]
      // }, {
      //   value: '字典表',
      //   label: '字典表'
      // }],
      typeoptions: [
        {
          value: '监测数据',
          label: '监测数据',
        },
        {
          value: '普通数据',
          label: '普通数据',
        },
        {
          value: '多媒体数据',
          label: '多媒体数据',
        },
        {
          value: '空间数据',
          label: '空间数据',
          children: [
            {
              value: '影响数据',
              label: '影响数据',
            },
            {
              value: '三维数据',
              label: '三维数据',
            },
            {
              value: '矢量数据',
              label: '矢量数据',
            },
          ],
        },
      ],
      tabletypeoptions: [
        {
          value: '地域',
          label: '地域',
        },
        {
          value: '时间',
          label: '时间',
        },
        {
          value: '空间',
          label: '空间',
        },
        {
          value: '文件',
          label: '文件',
        },
      ],
      // form表单
      form: {
        tableName: undefined,
        tableComment: undefined,
        directoryId: undefined,
        elementType: undefined,
        modelType: undefined,
        effectiveStartTime: undefined,
        effectiveEndTime: undefined,
        entity: undefined,
        tableSecurityLevel: undefined,
        description: undefined,
      },
      rules: {
        tableName: [{ required: true, message: '请输入英文名称', trigger: 'blur' }],
        tableComment: [{ required: true, message: '请输入中文名称', trigger: 'blur' }],
        directoryId: [{ required: true, message: '请选择数据目录', trigger: 'blur' }],
        elementType: [{ required: true, message: '请选择数据类型', trigger: 'blur' }],
        modelType: [{ required: true, message: '请选择模型类型', trigger: 'blur' }],
        effectiveStartTime: [{ required: true, message: '请选择生效开始时间', trigger: 'blur' }],
        tableSecurityLevel: [{ required: true, message: '请选择安全等级', trigger: 'blur' }],
      },
      value1: '',
      meunsoptions: [],
      columnModelEntities: [
        {
          column: 0,
          columnName: '',
          columnComment: '',
          dataLength: '',
          columnType: '', //字段
          columnKey: 0, //主键
          forginId: 0, //外键
          columnNullable: 0, //不能为空
          unit: '',
          dataDefault: '',
          isKV: 0, //是否是键值
          columnSecurityLevel: '', //安全等级
          dictV: '',
          dataType: '',
          dataLevel: '',
          dataFormat: '',
          columnDescription: '',
          mapKnowledge: 0,
        },
      ],
      editID: '',
      active: 1,
      //下拉框
      value: '',
      options: [],
      ziduanvalue: '',
      ziduanoptions: [
        {
          label: 'bit',
          value: 'bit',
        },
        {
          label: 'varchar2',
          value: 'varchar2',
        },
        {
          label: 'number',
          value: 'number',
        },
        {
          label: 'bool',
          value: 'bool',
        },
        {
          label: 'box',
          value: 'box',
        },
        {
          label: 'bytea',
          value: 'bytea',
        },
        {
          label: 'char',
          value: 'char',
        },
        {
          label: 'cidr',
          value: 'cidr',
        },
        {
          label: 'circle',
          value: 'circle',
        },
        {
          label: 'date',
          value: 'date',
        },
        {
          label: 'float4',
          value: 'float4',
        },
        {
          label: 'float8',
          value: 'float8',
        },
        {
          label: 'inet',
          value: 'inet',
        },
        {
          label: 'int2',
          value: 'int2',
        },
        {
          label: 'int4',
          value: 'int4',
        },
        {
          label: 'int8',
          value: 'int8',
        },
        {
          label: 'interval',
          value: 'interval',
        },
        {
          label: 'json',
          value: 'json',
        },
        {
          label: 'line',
          value: 'line',
        },

        {
          label: 'money',
          value: 'money',
        },
        {
          label: 'mumeric',
          value: 'mumeric',
        },
        {
          label: 'path',
          value: 'path',
        },
        {
          label: 'point',
          value: 'point',
        },
        {
          label: 'polygon',
          value: 'polygon',
        },
        {
          label: 'text',
          value: 'text',
        },
        {
          label: 'time',
          value: 'time',
        },
        {
          label: 'timestamp',
          value: 'timestamp',
        },
        {
          label: 'timestamptz',
          value: 'timestamptz',
        },
        {
          label: 'timetz',
          value: 'timetz',
        },
        {
          label: 'uuid',
          value: 'uuid',
        },
        {
          label: 'varbit',
          value: 'varbit',
        },
        {
          label: 'varchar',
          value: 'varchar',
        },
        {
          label: 'xml',
          value: 'xml',
        },
      ],
      //接口
      searchArr: {
        ancestralId: 4,
        page: 1,
        limit: 25,
      },
      majoroptions: [
        {
          label: '是',
          value: 1,
        },
        {
          label: '否',
          value: 0,
        },
      ],
      list: [],
      anquanions: [
        { label: '机密', value: 1 },
        { label: '秘密', value: 2 },
        {
          label: '外部公开',
          value: 4,
        },
        { label: '内部公开', value: 3 },
      ],
    }
  },

  mounted() {
    this.fun()
  },
  watch: {
    eitdValue: function (newval, oldVal) {
      if (newval) {
        this.columnModelEntities = []
        this.editID = newval.id
        console.log(this.securityoptions, 'watch')
        let time = ''
        if (newval.effectiveEndTime == 'NaN' || newval.effectiveEndTime == 'Invalid date' || newval.effectiveEndTime == '无期限') {
          time = '无期限'
        } else {
          time = this.$moment(new Date(newval.effectiveEndTime * 1000)).format('YYYY-MM-DD')
        }
        this.form.tableName = newval.tableName
        this.form.tableComment = newval.tableComment
        this.form.directoryId = newval.directoryId
        this.form.elementType = newval.elementType.split('/')
        this.form.modelType = newval.modelType.split('/')
          ; (this.form.effectiveStartTime = this.$moment(new Date(newval.effectiveStartTime * 1000)).format('YYYY-MM-DD')), (this.form.effectiveEndTime = time)
        this.form.entity = newval.entity
        this.securityoptions.map((v, i) => {
          if (v.value == newval.tableSecurityLevel) {
            this.form.tableSecurityLevel = v.label
          }
        })
        this.form.description = newval.description
        this.form.columnName = newval.columnName

        newval.columnModelEntities.map((v, i) => {
          this.columnModelEntities.push({
            columnName: v.columnName,
            columnComment: v.columnComment,
            dataLength: v.dataLength,
            columnType: this.onoroff(this.ziduanoptions, v.columnType), //字段
            columnKey: this.onoroff(this.majoroptions, v.columnKey), //主键
            forginId: this.onoroff(this.majoroptions, v.forginId), //外键
            columnNullable: this.onoroff(this.majoroptions, v.columnNullable), //不能为空
            unit: v.unit,
            dataDefault: v.dataDefault,
            isKV: this.onoroff(this.majoroptions, v.isKV), //是否是键值
            columnSecurityLevel: this.onoroff(this.anquanions, v.columnSecurityLevel), //安全等级
            dictV: '',
            dataType: this.onoroff(this.tabletypeoptions, v.dataType),
            dataLevel: '',
            dataFormat: '',
            columnDescription: v.columnDescription,
            mapKnowledge: this.onoroff(this.majoroptions, v.mapKnowledge),
          })
        })
        console.log(this.columnModelEntities, 111111111111111)
      }
    },
  },
  methods: {
    fun() {
      treeListApi({
        page: 1,
        limit: 25,
        ancestralId: 18,
      }).then((res) => {
        this.treeList = res.data.data
      })
    },
    onoroff(list, value) {
      let title = ''
      list.map((v, i) => {
        if (v.value == value) {
          title = v.label
        }
      })
      return title
    },
    onvalueoroffvalue(list, value) {
      let title = ''
      list.map((v, i) => {
        if (v.label == value) {
          title = v.value
        }
      })
      return title
    },
    close(val) {
      this.$refs.cascaderHandle1.dropDownVisible = false
      this.$refs.cascaderHandle2.dropDownVisible = false
      this.$refs.cascaderHandle3.dropDownVisible = false
    },
    orgChange1(row) {
      this.$refs.cascaderHandle1.dropDownVisible = false //监听值发生变化就关闭它
    },
    orgChange2(row) {
      this.$refs.cascaderHandle2.dropDownVisible = false //监听值发生变化就关闭它
    },
    orgChange3(row) {
      this.$refs.cascaderHandle3.dropDownVisible = false //监听值发生变化就关闭它
    },
    nextClick() {
      let effectiveStartTime = dayjs(this.form.effectiveStartTime).format('YYYY-MM-DD HH:DD:ss')
      let effectiveEndTime = dayjs(this.form.effectiveEndTime).format('YYYY-MM-DD HH:DD:ss')

      if (this.form.effectiveEndTime == undefined) {
        this.active = 2
      } else {
        if (effectiveStartTime <= effectiveEndTime) {
          this.active = 2
        } else {
          this.$message.error('开始时间不能超过结束时间')
        }
      }
    },
    root() {
      // form表单
      ; (this.form = {
        tableName: undefined,
        tableComment: undefined,
        directoryId: undefined,
        elementType: undefined,
        modelType: undefined,
        effectiveStartTime: undefined,
        effectiveEndTime: undefined,
        entity: undefined,
        tableSecurityLevel: undefined,
        description: undefined,
        // columnName: undefined,
      }),
        (this.columnModelEntities = [
          {
            column: 0,
            columnName: undefined,
            columnComment: '',
            dataLength: '',
            columnType: '', //字段
            columnKey: 0, //主键
            forginId: 0, //外键
            columnNullable: 0, //不能为空
            unit: '',
            dataDefault: '',
            isKV: 0, //是否是键值
            columnSecurityLevel: '', //安全等级
            dictV: '',
            dataType: '',
            dataLevel: '',
            dataFormat: '',
            columnDescription: '',
            mapKnowledge: 0,
          },
        ])
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.index == 'add') {
            let time = ''
            if (this.form.effectiveEndTime == null || this.form.effectiveEndTime == 'Invalid date' || this.form.effectiveEndTime == '无期限') {
              time = '无期限'
            } else {
              time = Math.floor(new Date(this.form.effectiveEndTime).getTime() / 1000)
            }

            let arr = {
              type: 1,
              name: this.form.tableName,
              directoryDescription: this.form.tableComment,
              parentId: Array.isArray(this.form.directoryId) ? this.form.directoryId[this.form.directoryId.length - 1] : this.form.directoryId,
              isDirectory: 1,
              ancestralId: 18,
              releaseStatus: 2,
              tableName: this.form.tableName,
              tableComment: this.form.tableComment,
              directoryId: Array.isArray(this.form.directoryId) ? this.form.directoryId[this.form.directoryId.length - 1] : this.form.directoryId,
              elementType: this.form.elementType.length > 1 ? this.form.elementType.join('/') : this.form.elementType[0],
              modelType: this.form.modelType.length > 1 ? this.form.modelType.join('/') : this.form.modelType[0],
              effectiveStartTime: Math.floor(new Date(this.form.effectiveStartTime).getTime() / 1000),
              effectiveEndTime: time,
              entity: this.form.entity,
              tableSecurityLevel: this.form.tableSecurityLevel,
              description: this.form.description,
            }
            this.list = []
            this.columnModelEntities.map((v, i) => {
              let err = {
                columnSecurityLevel: v.columnSecurityLevel,
                column: v.column,
                columnName: v.columnName,
                columnComment: v.columnComment,
                dataLength: v.dataLength,
                columnType: v.columnType, //字段
                columnKey: v.columnKey, //主键
                forginId: v.forginId, //外键
                columnNullable: v.columnNullable, //不能为空
                unit: v.unit,
                dataDefault: v.dataDefault,
                isKV: v.isKV, //是否是键值
                securityLevel: v.securityLevel, //安全等级
                dictV: v.dictV,
                dataType: v.dataType.length ? v.dataType.join('/') : '',
                dataLevel: v.dataLevel,
                dataFormat: v.dataFormat,
                columnDescription: v.columnDescription,
                mapKnowledge: v.mapKnowledge,
              }
              this.list.push(err)
            })

            addtableLise({ ...arr, columnModelEntities: this.list }).then((res) => {
              if (res.data.code == 0) {
                this.$message({
                  message: '添加成功',
                  type: 'success',
                })
                this.active = 1
                this.off()
                this.root()
              }
            })
          } else if (this.index == 'copy') {
            let time = ''
            if (this.form.effectiveEndTime == null || this.form.effectiveEndTime == 'Invalid date' || this.form.effectiveEndTime == '无期限') {
              time = '无期限'
            } else {
              time = Math.floor(new Date(this.form.effectiveEndTime).getTime() / 1000)
            }

            if (this.form.elementType == Array && this.form.modelType == Array) {
              this.form.elementType = this.form.elementType.split('/')
              this.form.modelType = this.form.modelType.split('/')
            }
            let value = ''
            this.securityoptions.map((v, i) => {
              if (this.form.tableSecurityLevel == '1') {
                value = '1'
              } else if (this.form.tableSecurityLevel == '2') {
                value = '2'
              } else if (this.form.tableSecurityLevel == '3') {
                value = '3'
              } else if (this.form.tableSecurityLevel == '4') {
                value = '4'
              } else if (this.form.tableSecurityLevel == v.label) {
                value = v.value
              }
            })
            let arr = {
              type: 1,
              name: this.form.tableName,
              directoryDescription: this.form.tableComment,
              parentId: Array.isArray(this.form.directoryId) ? this.form.directoryId[this.form.directoryId.length - 1] : this.form.directoryId,
              isDirectory: 1,
              ancestralId: 18,
              releaseStatus: 2,
              tableName: this.form.tableName,
              tableComment: this.form.tableComment,
              directoryId: Array.isArray(this.form.directoryId) ? this.form.directoryId[this.form.directoryId.length - 1] : this.form.directoryId,
              elementType: Array.isArray(this.form.elementType) ? this.form.elementType.join('/') : this.form.elementType,
              modelType: Array.isArray(this.form.modelType) ? this.form.modelType.join('/') : this.form.modelType,
              effectiveStartTime: Math.floor(new Date(this.form.effectiveStartTime).getTime() / 1000),
              effectiveEndTime: time,
              entity: this.form.entity,
              tableSecurityLevel: value,
              description: this.form.description,
            }
            this.list = []
            this.columnModelEntities.map((v, i) => {
              let err = {
                columnSecurityLevel: v.columnSecurityLevel,
                column: v.column,
                columnName: v.columnName,
                columnComment: v.columnComment,
                dataLength: v.dataLength,
                columnType: v.columnType, //字段
                columnKey: v.columnKey, //主键
                forginId: v.forginId, //外键
                columnNullable: v.columnNullable, //不能为空
                unit: v.unit,
                dataDefault: v.dataDefault,
                isKV: v.isKV, //是否是键值
                securityLevel: v.securityLevel, //安全等级
                dictV: v.dictV,
                dataType: v.dataType.length ? v.dataType.join('/') : '',
                dataLevel: v.dataLevel,
                dataFormat: v.dataFormat,
                columnDescription: v.columnDescription,
                mapKnowledge: v.mapKnowledge,
              }
              this.list.push(err)
            })

            addtableLise({ ...arr, columnModelEntities: this.list }).then((res) => {
              if (res.data.code == 0) {
                this.$message({
                  message: '添加成功',
                  type: 'success',
                })
                this.active = 1
                this.off()
                this.root()
              }
            })
          } else if (this.index == 'edit') {
            let time = ''
            if (this.form.effectiveEndTime == null || this.form.effectiveEndTime == 'Invalid date' || this.form.effectiveEndTime == '无期限') {
              time = '无期限'
            } else {
              time = Math.floor(new Date(this.form.effectiveEndTime).getTime() / 1000)
            }

            if (this.form.elementType == Array && this.form.modelType == Array) {
              this.form.elementType = this.form.elementType.split('/')
              this.form.modelType = this.form.modelType.split('/')
            }
            let value = ''
            this.securityoptions.map((v, i) => {
              if (this.form.tableSecurityLevel == '1') {
                value = '1'
              } else if (this.form.tableSecurityLevel == '2') {
                value = '2'
              } else if (this.form.tableSecurityLevel == '3') {
                value = '3'
              } else if (this.form.tableSecurityLevel == '4') {
                value = '4'
              } else if (this.form.tableSecurityLevel == v.label) {
                value = v.value
              }
            })
            let arr = {
              type: 1,
              id: this.editID,
              name: this.form.tableName,
              directoryDescription: this.form.tableComment,
              parentId: Array.isArray(this.form.directoryId) ? this.form.directoryId[this.form.directoryId.length - 1] : this.form.directoryId,
              isDirectory: 1,
              ancestralId: 18,
              releaseStatus: 2,
              tableName: this.form.tableName,
              tableComment: this.form.tableComment,
              directoryId: Array.isArray(this.form.directoryId) ? this.form.directoryId[this.form.directoryId.length - 1] : this.form.directoryId,
              elementType: Array.isArray(this.form.elementType) ? this.form.elementType.join('/') : this.form.elementType,
              modelType: Array.isArray(this.form.modelType) ? this.form.modelType.join('/') : this.form.modelType,
              effectiveStartTime: Math.floor(new Date(this.form.effectiveStartTime).getTime() / 1000),
              effectiveEndTime: time,
              entity: this.form.entity,
              tableSecurityLevel: value,
              description: this.form.description,
            }
            this.list = []
            this.columnModelEntities.map((v, i) => {
              let err = {
                id: this.editID,
                columnName: v.columnName,
                columnComment: v.columnComment,
                dataLength: v.dataLength,
                columnType: this.onvalueoroffvalue(this.ziduanoptions, v.columnType), //字段
                columnKey: this.onvalueoroffvalue(this.majoroptions, v.columnKey), //主键
                forginId: this.onvalueoroffvalue(this.majoroptions, v.forginId), //外键
                columnNullable: this.onvalueoroffvalue(this.majoroptions, v.columnNullable), //不能为空
                unit: v.unit,
                dataDefault: v.dataDefault,
                isKV: this.onvalueoroffvalue(this.majoroptions, v.isKV), //是否是键值
                columnSecurityLevel: this.onvalueoroffvalue(this.anquanions, v.columnSecurityLevel), //安全等级
                dictV: v.dictV,
                dataType: this.onvalueoroffvalue(this.tabletypeoptions, v.dataType),
                dataLevel: v.dataLevel,
                dataFormat: v.dataFormat,
                columnDescription: v.columnDescription,
                mapKnowledge: this.onvalueoroffvalue(this.anquanions, v.mapKnowledge),
              }
              this.list.push(err)
            })
            console.log(this.list)
            updatatableLise({ ...arr, columnModelEntities: this.list }).then((res) => {
              if (res.data.code == 0) {
                this.$message({
                  message: '更新成功',
                  type: 'success',
                })
                this.active = 1
                this.off()
                this.root()
              }
            })
          }
        } else {
          this.active = 1
          console.log('error submit!!')
          return false
        }
      })
    },
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let time = ''
          if (this.index == 'add') {
            if (this.form.effectiveEndTime == null || this.form.effectiveEndTime == 'Invalid date' || this.form.effectiveEndTime == '无期限') {
              time = '无期限'
            } else {
              time = Math.floor(new Date(this.form.effectiveEndTime).getTime() / 1000)
            }

            let arr = {
              name: this.form.tableName,
              directoryDescription: this.form.tableComment,
              parentId: Array.isArray(this.form.directoryId) ? this.form.directoryId[this.form.directoryId.length - 1] : this.form.directoryId,
              isDirectory: 1,
              ancestralId: 18,
              releaseStatus: 0,
              tableName: this.form.tableName,
              tableComment: this.form.tableComment,
              directoryId: this.form.directoryId[this.form.directoryId.length - 1],
              elementType: Array.isArray(this.form.elementType) ? this.form.elementType.join('/') : this.form.elementType,
              modelType: Array.isArray(this.form.modelType) ? this.form.modelType.join('/') : this.form.modelType,
              effectiveStartTime: Math.floor(new Date(this.form.effectiveStartTime).getTime() / 1000),
              effectiveEndTime: time,
              entity: this.form.entity,
              tableSecurityLevel: this.form.tableSecurityLevel,
              description: this.form.description,
            }
            this.list = []
            this.columnModelEntities.map((v, i) => {
              let err = {
                columnSecurityLevel: v.columnSecurityLevel,
                column: v.column,
                columnName: v.columnName,
                columnComment: v.columnComment,
                dataLength: v.dataLength,
                columnType: v.columnType, //字段
                columnKey: v.columnKey, //主键
                forginId: v.forginId, //外键
                columnNullable: v.columnNullable, //不能为空
                unit: v.unit,
                dataDefault: v.dataDefault,
                isKV: v.isKV, //是否是键值
                securityLevel: v.securityLevel, //安全等级
                dictV: v.dictV,
                dataType: v.dataType.length ? v.dataType.join('/') : '',
                dataLevel: v.dataLevel,
                dataFormat: v.dataFormat,
                columnDescription: v.columnDescription,
                mapKnowledge: v.mapKnowledge,
              }
              this.list.push(err)
            })
            addtableLise({ ...arr, columnModelEntities: this.list }).then((res) => {
              if (res.data.code == 0) {
                this.$message({
                  message: '保存成功',
                  type: 'success',
                })
                this.active = 1
                this.off()
                this.root()
              }
            })
          } else if (this.index == 'copy') {
            console.log(this.form)
            let time = ''
            if (this.form.effectiveEndTime == null || this.form.effectiveEndTime == 'Invalid date' || this.form.effectiveEndTime == '无期限') {
              time = '无期限'
            } else {
              time = Math.floor(new Date(this.form.effectiveEndTime).getTime() / 1000)
            }

            if (this.form.elementType == Array && this.form.modelType == Array) {
              this.form.elementType = this.form.elementType.split('/')
              this.form.modelType = this.form.modelType.split('/')
            }
            let value = ''
            this.securityoptions.map((v, i) => {
              if (this.form.tableSecurityLevel == '1') {
                value = '1'
              } else if (this.form.tableSecurityLevel == '2') {
                value = '2'
              } else if (this.form.tableSecurityLevel == '3') {
                value = '3'
              } else if (this.form.tableSecurityLevel == '4') {
                value = '4'
              } else if (this.form.tableSecurityLevel == v.label) {
                value = v.value
              }
            })
            let arr = {
              name: this.form.tableName,
              directoryDescription: this.form.tableComment,
              parentId: Array.isArray(this.form.directoryId) ? this.form.directoryId[this.form.directoryId.length - 1] : this.form.directoryId,
              isDirectory: 1,
              ancestralId: 18,
              releaseStatus: 0,
              tableName: this.form.tableName,
              tableComment: this.form.tableComment,
              directoryId: Array.isArray(this.form.directoryId) ? this.form.directoryId[this.form.directoryId.length - 1] : this.form.directoryId,
              elementType: Array.isArray(this.form.elementType) ? this.form.elementType.join('/') : this.form.elementType,
              modelType: Array.isArray(this.form.modelType) ? this.form.modelType.join('/') : this.form.modelType,
              effectiveStartTime: Math.floor(new Date(this.form.effectiveStartTime).getTime() / 1000),
              effectiveEndTime: time,
              entity: this.form.entity,
              tableSecurityLevel: value,
              description: this.form.description,
            }
            this.list = []
            this.columnModelEntities.map((v, i) => {
              let err = {
                columnSecurityLevel: v.columnSecurityLevel,
                column: v.column,
                columnName: v.columnName,
                columnComment: v.columnComment,
                dataLength: v.dataLength,
                columnType: v.columnType, //字段
                columnKey: v.columnKey, //主键
                forginId: v.forginId, //外键
                columnNullable: v.columnNullable, //不能为空
                unit: v.unit,
                dataDefault: v.dataDefault,
                isKV: v.isKV, //是否是键值
                securityLevel: v.securityLevel, //安全等级
                dictV: v.dictV,
                dataType: v.dataType.length ? v.dataType.join('/') : '',
                dataLevel: v.dataLevel,
                dataFormat: v.dataFormat,
                columnDescription: v.columnDescription,
                mapKnowledge: v.mapKnowledge,
              }
              this.list.push(err)
            })

            addtableLise({ ...arr, columnModelEntities: this.list }).then((res) => {
              if (res.data.code == 0) {
                this.$message({
                  message: '保存成功',
                  type: 'success',
                })
                this.active = 1
                this.off()
                this.root()
              }
            })
          } else if (this.index == 'edit') {
            let time = ''
            if (this.form.effectiveEndTime == null || this.form.effectiveEndTime == 'Invalid date' || this.form.effectiveEndTime == '无期限') {
              time = '无期限'
            } else {
              time = Math.floor(new Date(this.form.effectiveEndTime).getTime() / 1000)
            }

            if (this.form.elementType == Array && this.form.modelType == Array) {
              this.form.elementType = this.form.elementType.split('/')
              this.form.modelType = this.form.modelType.split('/')
            }
            let value = ''
            this.securityoptions.map((v, i) => {
              if (this.form.tableSecurityLevel == '1') {
                value = '1'
              } else if (this.form.tableSecurityLevel == '2') {
                value = '2'
              } else if (this.form.tableSecurityLevel == '3') {
                value = '3'
              } else if (this.form.tableSecurityLevel == '4') {
                value = '4'
              } else if (this.form.tableSecurityLevel == v.label) {
                value = v.value
              }
            })
            let arr = {
              type: 1,
              id: this.editID,
              name: this.form.tableName,
              directoryDescription: this.form.tableComment,
              parentId: Array.isArray(this.form.directoryId) ? this.form.directoryId[this.form.directoryId.length - 1] : this.form.directoryId,
              isDirectory: 1,
              ancestralId: 18,
              releaseStatus: 0,
              tableName: this.form.tableName,
              tableComment: this.form.tableComment,
              directoryId: Array.isArray(this.form.directoryId) ? this.form.directoryId[this.form.directoryId.length - 1] : this.form.directoryId,
              elementType: Array.isArray(this.form.elementType) ? this.form.elementType.join('/') : this.form.elementType,
              modelType: Array.isArray(this.form.modelType) ? this.form.modelType.join('/') : this.form.modelType,
              effectiveStartTime: Math.floor(new Date(this.form.effectiveStartTime).getTime() / 1000),
              effectiveEndTime: time,
              entity: this.form.entity,
              tableSecurityLevel: value,
              description: this.form.description,
            }
            this.list = []
            this.columnModelEntities.map((v, i) => {
              let err = {
                id: this.editID,
                columnName: v.columnName,
                columnComment: v.columnComment,
                dataLength: v.dataLength,
                columnType: this.onvalueoroffvalue(this.ziduanoptions, v.columnType), //字段
                columnKey: this.onvalueoroffvalue(this.majoroptions, v.columnKey), //主键
                forginId: this.onvalueoroffvalue(this.majoroptions, v.forginId), //外键
                columnNullable: this.onvalueoroffvalue(this.majoroptions, v.columnNullable), //不能为空
                unit: v.unit,
                dataDefault: v.dataDefault,
                isKV: this.onvalueoroffvalue(this.majoroptions, v.isKV), //是否是键值
                columnSecurityLevel: this.onvalueoroffvalue(this.anquanions, v.columnSecurityLevel), //安全等级
                dictV: v.dictV,
                dataType: this.onvalueoroffvalue(this.tabletypeoptions, v.dataType),
                dataLevel: v.dataLevel,
                dataFormat: v.dataFormat,
                columnDescription: v.columnDescription,
                mapKnowledge: this.onvalueoroffvalue(this.anquanions, v.mapKnowledge),
              }
              this.list.push(err)
            })
            console.log(this.list)
            updatatableLise({ ...arr, columnModelEntities: this.list }).then((res) => {
              if (res.data.code == 0) {
                this.$message({
                  message: '更新成功',
                  type: 'success',
                })
                this.active = 1
                this.off()
                this.root()
              }
            })
          }
        } else {
          this.active = 1
          console.log('error submit!!')
          return false
        }
      })
    },
    adddataFun() {
      this.columnModelEntities.push({
        column: this.columnModelEntities[this.columnModelEntities.length - 1].column + 1,
        columnName: '',
        columnComment: '',
        dataLength: '',
        columnType: '', //字段
        columnKey: 0, //主键
        forginId: 0, //外键
        columnNullable: 0, //不能为空
        unit: '',
        dataDefault: '',
        isKV: 0, //是否是键值
        securityLevel: '', //安全等级
        dictV: '',
        dataType: '',
        dataLevel: '',
        dataFormat: '',
        columnDescription: '',
        mapKnowledge: '',
      })
    },
    jiandataFun(val) {
      this.columnModelEntities.splice(val, 1)
    },
    off() {
      this.$emit('dologdin', false)
    },
    // 弹框关闭
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          this.off()
        })
        .catch((_) => { })
    },
  },
}
</script>

<style lang="scss" scoped>
::v-deep(::-webkit-scrollbar) {
  display: block !important;
}
</style>
